package com.nowcoder.code.hjnc;

import java.util.Arrays;
import java.util.HashMap;

public class NC61 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(new NC61().twoSum(new int[]{3, 2, 4}, 6)));
    }

    public int[] twoSum(int[] numbers, int target) {

        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < numbers.length; i++) {
            if (map.get(target - numbers[i]) != null) {
                return new int[]{map.get(target - numbers[i] + 1), i + 1};
            }
            map.put(numbers[i], i);
        }

        return null;
    }

    public int[] twoSum1(int[] numbers, int target) {
        int[] old = Arrays.copyOf(numbers, numbers.length);
        Arrays.sort(numbers);
        int s = 0;
        int e = numbers.length - 1;
        while (s < e) {
            int sum = numbers[s] + numbers[e];
            if (sum == target) {
                break;
            } else if (sum > target) {
                e--;
            } else {
                s++;
            }
        }


        int[] result = new int[2];
        int j = 0;
        for (int i = 0; i < old.length && j <= 1; i++) {
            if (old[i] == numbers[s]) {
                result[j] = i + 1;
                j++;
            } else if (old[i] == numbers[e]) {
                result[j] = i + 1;
                j++;
            }
        }

        return result;
    }


}
